// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace CharityGamesOnline.Modules.Event
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for Event 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class EventController : ISearchable, IPortable
    {
        public const string EXPORT_DATE_FORMAT = "R";
        #region "Public Methods"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="PortalID">The Id of the portal</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public List<EventInfo> GetEvents(int PortalID)
        {
            return GetEvents(PortalID, 0);
        }        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="CharityID"></param>
        /// <returns></returns>
        public List<EventInfo> GetEvents(int PortalID, int CharityID)
        {
            return GetEvents(PortalID, CharityID, Null.NullDate, Null.NullDate);
        }

        public List<EventInfo> GetEvents(int PortalID, int CharityID, DateTime DateCurrent)
        {
            return GetEvents(PortalID, CharityID, DateCurrent, DateCurrent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="CharityID"></param>
        /// <param name="DateBegin"></param>
        /// <param name="DateEnd"></param>
        /// <returns></returns>
        public List<EventInfo> GetEvents(int PortalID, int CharityID, DateTime DateBegin, DateTime DateEnd)
        {
            return GetEvents(PortalID, CharityID, DateBegin, DateEnd, false, 0, 0);
        }

        public List<EventInfo> GetEvents(int PortalID, int CharityID, DateTime DateBegin, DateTime DateEnd, bool getCount, int pageSize, int currentPage)
        {
            return CBO.FillCollection<EventInfo>(DataProvider.Instance().GetEvents(PortalID, CharityID, DateBegin, DateEnd, getCount, pageSize, currentPage));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="EventID"></param>
        /// <returns></returns>
        public EventInfo GetEvent(int PortalID, int EventID)
        {
            return (EventInfo)CBO.FillObject(DataProvider.Instance().GetEvent(PortalID, EventID), typeof(EventInfo));
        }        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objEvent"></param>
        /// <returns></returns>
        public int SaveEvent(EventInfo objEvent)
        {
            return DataProvider.Instance().SaveEvent(objEvent.EventID, objEvent.PortalID, objEvent.LastUpdatedID, objEvent.DateBegin, objEvent.DateEnd, 
                    objEvent.Name, objEvent.Description, objEvent.MaxEnrollment, objEvent.EnrollFee, objEvent.EnrollType, 
                    objEvent.ImageURL, objEvent.ImageType, objEvent.ImageWidth, objEvent.ImageHeight, objEvent.Status, 
                    objEvent.CategoryID, objEvent.ImageDisplay, objEvent.Signups, objEvent.AllDayEvent, objEvent.Approved);            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="EventID"></param>
        public void DeleteEvent(int PortalID, int EventID)
        {
            DataProvider.Instance().DeleteEvent(PortalID, EventID);
        }

        public void AddEventCharity(int PortalID, int EventID, int CharityID)
        {
            DataProvider.Instance().AddEventCharity(PortalID, EventID, CharityID);
        }

        public void AddEventGame(int PortalID, int EventID, int GameID)
        {
            DataProvider.Instance().AddEventGame(PortalID, EventID, GameID);
        }

        public void DeleteEventCharity(int PortalID, int EventID)
        {
            DeleteEventCharity(PortalID, EventID, Null.NullInteger);
        }

        public void DeleteEventCharity(int PortalID, int EventID, int CharityID)
        {
            DataProvider.Instance().DeleteEventCharity(PortalID, EventID, CharityID);
        }

        public void DeleteEventGame(int PortalID, int EventID)
        {
            DeleteEventGame(PortalID, EventID, Null.NullInteger);
        }

        public void DeleteEventGame(int PortalID, int EventID, int GameID)
        {
            DataProvider.Instance().DeleteEventGame(PortalID, EventID, GameID);
        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            List<EventInfo> lstEvents = GetEvents(ModInfo.PortalID);
            foreach (EventInfo objEvent in lstEvents)
            {
                SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objEvent.Name, objEvent.CreatedByID, objEvent.LastUpdateDate, ModInfo.ModuleID, objEvent.EventID.ToString(), objEvent.Description);
                SearchItemCollection.Add(SearchItem);
            }

            return SearchItemCollection;            
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int PortalID)
        {

            string strXML = "";

            List<EventInfo> colEvents = GetEvents(PortalID);
            if (colEvents.Count != 0)
            {
                strXML += "<Events>";
                foreach (EventInfo objEvent in colEvents)
                {
                    strXML += "<Event>";
                    strXML += "<PortalID>" + XmlUtils.XMLEncode(objEvent.PortalID.ToString()) + "</PortalID>";
                    strXML += "<DateBegin>" + XmlUtils.XMLEncode(objEvent.DateBegin.ToString(EXPORT_DATE_FORMAT)) + "</DateBegin>";
                    strXML += "<DateEnd>" + XmlUtils.XMLEncode(objEvent.DateEnd.ToString(EXPORT_DATE_FORMAT)) + "</DateEnd>";
                    strXML += "<Name>" + XmlUtils.XMLEncode(objEvent.Name) + "</Name>";
                    strXML += "<Description>" + XmlUtils.XMLEncode(objEvent.Description) + "</Description>";
                    strXML += "<MaxEnrollment>" + XmlUtils.XMLEncode(objEvent.MaxEnrollment.ToString()) + "</MaxEnrollment>";
                    strXML += "<EnrollFee>" + XmlUtils.XMLEncode(objEvent.EnrollFee.ToString()) + "</EnrollFee>";
                    strXML += "<EnrollType>" + XmlUtils.XMLEncode(objEvent.EnrollType) + "</EnrollType>";
                    strXML += "<ImageURL>" + XmlUtils.XMLEncode(objEvent.ImageURL) + "</ImageURL>";
                    strXML += "<ImageType>" + XmlUtils.XMLEncode(objEvent.ImageType) + "</ImageType>";
                    strXML += "<ImageWidth>" + XmlUtils.XMLEncode(objEvent.ImageWidth.ToString()) + "</ImageWidth>";
                    strXML += "<ImageHeight>" + XmlUtils.XMLEncode(objEvent.ImageHeight.ToString()) + "</ImageHeight>";
                    strXML += "<CategoryID>" + XmlUtils.XMLEncode(objEvent.CategoryID.ToString()) + "</CategoryID>";
                    strXML += "<ImageDisplay>" + XmlUtils.XMLEncode(objEvent.ImageDisplay.ToString()) + "</ImageDisplay>";
                    strXML += "<Signups>" + XmlUtils.XMLEncode(objEvent.Signups.ToString()) + "</Signups>";
                    strXML += "<AllDayEvent>" + XmlUtils.XMLEncode(objEvent.AllDayEvent.ToString()) + "</AllDayEvent>";
                    strXML += "<Approved>" + XmlUtils.XMLEncode(objEvent.Approved.ToString()) + "</Approved>";
                    strXML += "<Approved>" + XmlUtils.XMLEncode(objEvent.Approved.ToString()) + "</Approved>";
                    strXML += "<Approved>" + XmlUtils.XMLEncode(objEvent.Approved.ToString()) + "</Approved>";
                    strXML += "<Approved>" + XmlUtils.XMLEncode(objEvent.Approved.ToString()) + "</Approved>";
                    strXML += "<Approved>" + XmlUtils.XMLEncode(objEvent.Approved.ToString()) + "</Approved>";
                    strXML += "</Event>";                    
                }
                strXML += "</Events>";
            }

            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be imported</param> 
        /// <param name="Content">The content to be imported</param> 
        /// <param name="Version">The version of the module to be imported</param> 
        /// <param name="UserId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {

            //XmlNode xmlEvents = Globals.GetContent(Content, "Events");
            //foreach (XmlNode xmlEvent in xmlEvents.SelectNodes("Event"))
            //{
            //    EventInfo objEvent = new EventInfo();
            //    objEvent.ModuleId = ModuleID;
            //    objEvent.Content = xmlEvent.SelectSingleNode("content").InnerText;
            //    objEvent.CreatedByUser = UserId;
            //    AddEvent(objEvent);
            //}

        }

        #endregion

    }
}